package view.dtvb;

import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.UIXTable;
import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.input.RichSelectOneChoice;
import oracle.adf.view.rich.component.rich.nav.RichCommandButton;
import oracle.adf.view.rich.component.rich.output.RichOutputText;
import oracle.adf.view.rich.context.AdfFacesContext;
import oracle.adf.view.rich.event.DialogEvent;
import oracle.adf.view.rich.event.PopupCanceledEvent;

import oracle.jbo.Row;
import oracle.jbo.RowSet;
import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;

import oracle.jbo.uicli.binding.JUCtrlRangeBinding;

import org.apache.myfaces.trinidad.event.SelectionEvent;
import org.apache.myfaces.trinidad.model.UploadedFile;
import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;

import view.common.Utils;

public class noidungvbBean {
    private RichPopup popupThemMoi;
    private RichPopup popupXemVb;
    private RichPopup popupDeleteVb;
    private RichPopup popupValidate;
    private UploadedFile _file;
    private RichOutputText inputtextOperator;
    private String sohieuvb;
    private RichInputText txtSohieuvb = new RichInputText();
    private String trichyeu;
    private String Title;
    private Date ngaybanhanhbd;
    private Date ngaybanhanhkt;
    private RichCommandButton buttonDownload;
    private RichSelectOneChoice selectdonvi;

    private boolean unZipFile = false; //false: Keep File, true: Un ZipFile
    private String fileDeleted;
    private UIComponent iter;
    private static boolean hasFile = false;
    private boolean fileExisted = false;
    private static ArrayList<String> currentFiles = new ArrayList<String>();
    private int pageNumber;
    private UIXTable tbl;
    private static final int RANGE_SIZE = 10;

    public noidungvbBean() {
        super();
        JUCtrlRangeBinding VanBanView = this.getVanBanView();
        VanBanView.getIteratorBinding().getNavigatableRowIterator().scrollRange(0);

        if (Boolean.FALSE.equals(Utils.getElExpression("#{adfFacesContext.postback}"))) {
            DCIteratorBinding iterVoidungvb =
                Utils.getIteratorBinding("DtvbNoidungvanbanView1Iterator");
            iterVoidungvb.getViewObject().getViewCriteriaManager().clearViewCriterias();
            iterVoidungvb.executeQuery();

            DCIteratorBinding iterNoidungVbCheckExist =
                Utils.getIteratorBinding("DtvbNoidungvanbanCheckExistView1Iterator");
            iterNoidungVbCheckExist.getViewObject().getViewCriteriaManager().clearViewCriterias();
            iterNoidungVbCheckExist.executeQuery();
        }
    }

    public List<SelectItem> getDsdonvi() {
        List<SelectItem> lstDsdonvi = new ArrayList<SelectItem>();
        try {
            DCIteratorBinding iterDsdonvi =
                Utils.getIteratorBinding("DtvbDonviView1Iterator");

            for (Row row : iterDsdonvi.getAllRowsInRange()) {
                lstDsdonvi.add(new SelectItem(row.getAttribute("Id").toString(),
                                              row.getAttribute("TenDonvi").toString()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lstDsdonvi;
    }

    public void downloadfile(ActionEvent actionEvent) {
        String fileName =
            (String)actionEvent.getComponent().getAttributes().get("fileName");
        buttonDownload.setText(fileName);
        FacesContext context = FacesContext.getCurrentInstance();
        ExtendedRenderKitService erks =
            Service.getService(context.getRenderKit(),
                               ExtendedRenderKitService.class);
        erks.addScript(context, "customHandler();");
    }

    public void dwdFile(FacesContext facesContext, OutputStream out) {
        File duongdan =
            new File(Utils.ENUM_CAU_HINH_KHOA.FILE_ROOT.getValue("HtCauHinhView1Iterator"),
                     Utils.ENUM_CAU_HINH_KHOA.DTVB_DIR.getValue("HtCauHinhView1Iterator"));
        File file = new File(duongdan + "/" + buttonDownload.getText());
        FileInputStream Fileinput = null;
        byte[] bt;
        try {
            Fileinput = new FileInputStream(file);
            int checkline;
            while ((checkline = Fileinput.available()) > 0) {
                bt = new byte[checkline];
                int rst = Fileinput.read(bt);
                out.write(bt, 0, bt.length);
                if (rst == -1)
                    break;

            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void ResetSearch() {
        try {
            DCIteratorBinding iterVoidungvb =
                Utils.getIteratorBinding("DtvbNoidungvanbanView1Iterator");
            iterVoidungvb.getViewObject().getViewCriteriaManager().clearViewCriterias();
            iterVoidungvb.executeQuery();

            DCIteratorBinding iterNoidungVbCheckExist =
                Utils.getIteratorBinding("DtvbNoidungvanbanCheckExistView1Iterator");
            iterNoidungVbCheckExist.getViewObject().getViewCriteriaManager().clearViewCriterias();
            iterNoidungVbCheckExist.executeQuery();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void searchNoidungVB() {
        try {
            if (getNgaybanhanhbd() != null && getNgaybanhanhkt() != null &&
                getNgaybanhanhbd().after(getNgaybanhanhkt())) {
                String meg = "Bạn phải nhập đến ngày lớn hơn từ ngày";
                Utils.showMessage(Utils.MSG_ERROR, meg);
            } else {
                DCIteratorBinding IteratorNoidungVB =
                    Utils.getIteratorBinding("DtvbNoidungvanbanView1Iterator");
                ViewObject vo = IteratorNoidungVB.getViewObject();
                ViewCriteria vc =
                    vo.getViewCriteriaManager().getViewCriteria("DtvbNoidungvanbanViewCriteria");
                vo.ensureVariableManager().setVariableValue("varsohieuvb",
                                                            getSohieuvb());
                vo.ensureVariableManager().setVariableValue("vartrichyeu",
                                                            getTrichyeu());
                vo.ensureVariableManager().setVariableValue("varNgaybd",
                                                            getNgaybanhanhbd());
                vo.ensureVariableManager().setVariableValue("varNgaykt",
                                                            getNgaybanhanhkt());
                vo.ensureVariableManager().setVariableValue("varDonvi",
                                                            selectdonvi.getValue());
                vo.getViewCriteriaManager().removeViewCriteria("DtvbNoidungvanbanViewCriteria");
                vo.getViewCriteriaManager().applyViewCriteria(vc);
                IteratorNoidungVB.executeQuery();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void create(ActionEvent actionEvent) {
        Utils.iteratorRefresh("DtvbNoidungvanbanView1Iterator");
        Utils.execute("CreateInsert");
        Utils.showPopup(popupThemMoi);
        inputtextOperator.setValue("0");
    }

    public void update(ActionEvent actionEvent) {
        DCIteratorBinding IteratorNoidungvb =
            Utils.getIteratorBinding("DtvbNoidungvanbanView1Iterator");
        DCIteratorBinding IteratorDmDonvi =
            Utils.getIteratorBinding("DtvbDonviView1Iterator");
        if (IteratorNoidungvb.getCurrentRow() == null) {
            String meg = "Chưa có văn bản nào.";
            Utils.showMessage(Utils.MSG_INFO, meg);
        } else {
            IteratorDmDonvi.setCurrentRowWithKeyValue(IteratorNoidungvb.getCurrentRow().getAttribute("DonViId").toString());
            if (IteratorNoidungvb.getCurrentRow().getAttribute("FileId") !=
                null) {
                hasFile = true;
                DCIteratorBinding fileIterator =
                    Utils.getIteratorBinding("HtFileView1Iterator");
                ViewObject vo = fileIterator.getViewObject();
                ViewCriteria vc = vo.createViewCriteria();
                ViewCriteriaRow cr = vc.createViewCriteriaRow();
                String varStr =
                    "IN (" + IteratorNoidungvb.getCurrentRow().getAttribute("FileId") +
                    ")";
                cr.setAttribute("Id", varStr);
                vc.addElement(cr);
                vo.getViewCriteriaManager().clearViewCriterias();
                vo.applyViewCriteria(vc);
                vo.executeQuery();
                String str[] =
                    IteratorNoidungvb.getCurrentRow().getAttribute("FileId").toString().split(",");
                if (str.length > 0) {
                    for (int i = 0; i < str.length; i++) {
                        currentFiles.add(str[i]);
                    }
                }
            } else {
                hasFile = false;
            }
            Utils.showPopup(popupThemMoi);
            inputtextOperator.setValue("1");
        }
    }

    public int checkExisted(String SohieuvbMoi, String Vanbanid) {
        DCIteratorBinding IteratorNoidungVB =
            Utils.getIteratorBinding("DtvbNoidungvanbanCheckExistView1Iterator");
        ViewObject vo = IteratorNoidungVB.getViewObject();
        ViewCriteria vc =
            vo.getViewCriteriaManager().getViewCriteria("DtvbNoidungvanbanCheckExistViewCriteria");
        vo.ensureVariableManager().setVariableValue("varSohieuvb",
                                                    SohieuvbMoi);
        vo.ensureVariableManager().setVariableValue("varCheckId", Vanbanid);
        vo.getViewCriteriaManager().removeViewCriteria("DtvbNoidungvanbanCheckExistViewCriteria");
        vo.getViewCriteriaManager().applyViewCriteria(vc);
        vo.setRangeSize(1);
        IteratorNoidungVB.executeQuery();
        if (vo.getAllRowsInRange().length > 0) {
            return 1;
        }
        return 0;
    }

    public void commitDialog(DialogEvent dialogEvent) {
        boolean fileupload = true;
        int operator = inputtextOperator.getValue().equals("1") ? 1 : 0;
        DCIteratorBinding IteratorNoidungvb =
            Utils.getIteratorBinding("DtvbNoidungvanbanView1Iterator");
        String VanbanId = "";
        String SohieuvbMoi = "";
        SohieuvbMoi = txtSohieuvb.getValue().toString();
        if (operator == 0) {
            VanbanId = "";
        } else {
            VanbanId =
                    IteratorNoidungvb.getCurrentRow().getAttribute("Id").toString();
        }
        if (checkExisted(SohieuvbMoi, VanbanId) > 0) {
            String meg =
                "Số hiệu văn bản đã tồn tại. Bạn phải nhập số hiệu văn bản khác";
            Utils.showMessage(Utils.MSG_ERROR, meg);
        } else {
            DCIteratorBinding DonviIterrator =
                Utils.getIteratorBinding("DtvbDonviView1Iterator");
            String DonviId =
                DonviIterrator.getCurrentRow().getAttribute("Id").toString();
            IteratorNoidungvb.getCurrentRow().setAttribute("DonViId", DonviId);
            ArrayList<String> fileList = new ArrayList<String>();
            if (this.getFile() != null) {
                UploadedFile file = this.getFile();
                String tenfile = file.getFilename();
                if (Utils.checkDefaultFileExtension(tenfile)) {
                    String dirRoot =
                        Utils.ENUM_CAU_HINH_KHOA.FILE_ROOT.getValue("HtCauHinhView1Iterator"); //Thu muc file goc
                    String dirFile =
                        Utils.ENUM_CAU_HINH_KHOA.DTVB_DIR.getValue("HtCauHinhView1Iterator"); //duong dan tuong doi thu muc file
                    File dirFilePath =
                        new File(Utils.ENUM_CAU_HINH_KHOA.DTVB_DIR.getValue("HtCauHinhView1Iterator")); //duong dan tuong doi thu muc file
                    File dir =
                        new File(dirRoot, dirFile); //Duong dan tuyet doi thu muc file
                    String fileId = null;
                    DCIteratorBinding fileIterator1 =
                        Utils.getIteratorBinding("HtFileView1Iterator");
                    if (this.unZipFile) {
                        try {
                            String ext =
                                _file.getFilename().substring(_file.getFilename().lastIndexOf("."),
                                                              _file.getFilename().length());
                            if (ext.equals(".zip")) {
                                ZipInputStream zipInputStream =
                                    new ZipInputStream(_file.getInputStream());
                                ZipEntry zipEntry =
                                    zipInputStream.getNextEntry();
                                while (zipEntry != null) {
                                    Utils.execute("HtFileInsert");
                                    fileId =
                                            fileIterator1.getCurrentRow().getAttribute("Id").toString();
                                    if (zipEntry.isDirectory() == false) {
                                        File fileName =
                                            fileName = new File(fileId + "_" +
                                                                zipEntry.getName());
                                        File fileEx =
                                            new File(dir, fileName.toString());
                                        File filePath =
                                            new File(dirFile, fileName.toString());
                                        if (fileEx.getParentFile().exists() ==
                                            false) {
                                            fileEx.getParentFile().mkdirs();
                                        }
                                        FileOutputStream fileExOut =
                                            new FileOutputStream(fileEx);
                                        byte[] buffer = new byte[1024];
                                        int len = 0;
                                        while ((len =
                                                zipInputStream.read(buffer)) !=
                                               -1) {
                                            fileExOut.write(buffer, 0, len);
                                        }
                                        fileIterator1.getCurrentRow().setAttribute("TenFile",
                                                                                   fileName.toString());
                                        fileIterator1.getCurrentRow().setAttribute("DuongDan",
                                                                                   filePath);
                                        fileIterator1.getCurrentRow().setAttribute("MaUngDung",
                                                                                   Utils.ENUM_MA_UNG_DUNG.DTVB.getMaUngDung());
                                        fileList.add(fileId.toString());
                                        fileExOut.flush();
                                        fileExOut.close();
                                    }
                                    zipEntry = zipInputStream.getNextEntry();
                                }
                                zipInputStream.close();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (fileList.size() > 0) {
                            String fileUploadedId = "";
                            for (int i = 0; i <= fileList.size() - 1; i++) {
                                fileUploadedId += fileList.get(i) + ",";
                            }
                            fileUploadedId =
                                    fileUploadedId.substring(0, fileUploadedId.lastIndexOf(","));
                            IteratorNoidungvb.getCurrentRow().setAttribute("FileId",
                                                                           fileUploadedId);
                        }
                    } else {
                        Utils.execute("HtFileInsert");
                        fileId =
                                fileIterator1.getCurrentRow().getAttribute("Id").toString();
                        if (Utils.fileUpload(this.getFile(), dir, fileId)) {
                            Utils.processUpload(this.getFile(), dirFilePath,
                                                "HtFileView1Iterator",
                                                "DtvbNoidungvanbanView1Iterator",
                                                operator,
                                                Utils.ENUM_MA_UNG_DUNG.DTVB,
                                                fileId);
                        }
                    }
                } else {
                    this.setFile(null);
                    String message =
                        "<html>Hệ thống hỗ trợ file có định dạng: *.doc,*.docx,*.xls,*.xlsx,<p>*.ppt,*.pptx,*.odt,*.ods,*.odp,*.pdf,*.zip,*.rar,*.txt</p></html>";
                    Utils.showMessage(Utils.MSG_ERROR, message);
                    fileupload = false;
                }
            } else {
                if (operator == 1) {
                    String fileUploadedId = "";
                    if (currentFiles.size() > 0) {
                        for (int i = 0; i < currentFiles.size(); i++) {
                            fileUploadedId += currentFiles.get(i) + ",";
                        }
                        fileUploadedId =
                                fileUploadedId.substring(0, fileUploadedId.lastIndexOf(","));
                    }
                    IteratorNoidungvb.getCurrentRow().setAttribute("FileId",
                                                                   fileUploadedId);
                }
            }
            if (fileupload) {
                String rowKey = "";
                if (IteratorNoidungvb.getCurrentRow() != null) {
                    rowKey =
                            IteratorNoidungvb.getCurrentRow().getKey().toStringFormat(false);
                }
                Utils.execute("Commit");
                //Utils.iteratorRefresh("DtvbNoidungvanbanView1Iterator");
                Utils.iteratorRefresh("DtvbNoidungvanbanCheckExistView1Iterator");
                Utils.iteratorRefresh("HtFileView1Iterator");
                IteratorNoidungvb.setCurrentRowWithKey(rowKey);
                if (operator == 0) {
                    ViewObject vo = IteratorNoidungvb.getViewObject();
                    ViewCriteria vc =
                        vo.getViewCriteriaManager().getViewCriteria("DtvbNoidungvanbanViewCriteria");
                    vo.ensureVariableManager().setVariableValue("varsohieuvb",
                                                                "%");
                    vo.ensureVariableManager().setVariableValue("vartrichyeu",
                                                                "%");
                    vo.ensureVariableManager().setVariableValue("varNgaybd",
                                                                "");
                    vo.ensureVariableManager().setVariableValue("varNgaykt",
                                                                "");
                    vo.ensureVariableManager().setVariableValue("varDonvi",
                                                                "");
                    vo.getViewCriteriaManager().removeViewCriteria("DtvbNoidungvanbanViewCriteria");
                    vo.getViewCriteriaManager().applyViewCriteria(vc);
                    IteratorNoidungvb.executeQuery();
                }
                hasFile = false;
                currentFiles.clear();
                this.unZipFile = false;
            }
        }
    }

    public void deleteFile(ActionEvent actionEvent) {
        DCIteratorBinding fileIterator =
            Utils.getIteratorBinding("HtFileView1Iterator");
        fileIterator.setCurrentRowWithKeyValue(fileDeleted);
        fileIterator.removeCurrentRow();
        for (int i = 0; i < currentFiles.size(); i++) {
            if (currentFiles.contains(fileDeleted)) {
                currentFiles.remove(fileDeleted);
            }
        }
        AdfFacesContext.getCurrentInstance().addPartialTarget(iter);
    }

    public void cancelDiaglog(PopupCanceledEvent popupCanceledEvent) {
        try {
            DCIteratorBinding iterNoidungvb =
                Utils.getIteratorBinding("DtvbNoidungvanbanView1Iterator");
            String rowKey = "";
            if (iterNoidungvb.getCurrentRow() != null) {
                rowKey =
                        iterNoidungvb.getCurrentRow().getKey().toStringFormat(false);
            }
            Utils.execute("Rollback");
            //Utils.iteratorRefresh("DtvbNoidungvanbanView1Iterator");
            Utils.iteratorRefresh("DtvbNoidungvanbanCheckExistView1Iterator");
            Utils.iteratorRefresh("HtFileView1Iterator");
            iterNoidungvb.setCurrentRowWithKey(rowKey);
            this.unZipFile = false;
            hasFile = false;
            currentFiles.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteDiaglog(DialogEvent dialogEvent) {
        try {
            DCIteratorBinding iterNoidungvb =
                Utils.getIteratorBinding("DtvbNoidungvanbanView1Iterator");
            String VanbanId =
                iterNoidungvb.getCurrentRow().getAttribute("Id").toString();
            ViewObject vo = iterNoidungvb.getViewObject();
            vo.setAccessMode(RowSet.SCROLLABLE);
            iterNoidungvb.setCurrentRowWithKeyValue(VanbanId);
            if (iterNoidungvb.getCurrentRow() == null) {
                String meg = "Bạn chưa chọn văn bản nào.";
                Utils.showMessage(Utils.MSG_INFO, meg);
            } else {
                if (iterNoidungvb.getCurrentRow().getAttribute("FileId") !=
                    null) {
                    DCIteratorBinding fileIterator =
                        Utils.getIteratorBinding("HtFileView1Iterator");
                    File filePath =
                        new File(Utils.ENUM_CAU_HINH_KHOA.FILE_ROOT.getValue("HtCauHinhView1Iterator"),
                                 "/");
                    String FileId =
                        iterNoidungvb.getCurrentRow().getAttribute("FileId").toString();
                    fileIterator.setCurrentRowWithKeyValue(FileId);
                    String duongdan =
                        fileIterator.getCurrentRow().getAttribute("DuongDan").toString();
                    File oldPath = new File(filePath, duongdan);
                    Utils.fileDelete(oldPath);

                    String str[] = FileId.toString().split(",");
                    if (str.length > 0) {
                        for (int i = 0; i < str.length; i++) {
                            currentFiles.add(str[i]);
                        }
                    }
                    for (int i = 0; i < currentFiles.size(); i++) {
                        fileIterator.setCurrentRowWithKeyValue(currentFiles.get(i));
                        fileIterator.removeCurrentRow();
                    }
                }
                Utils.execute("Delete");
                List constrainExist = Utils.execute("Commit");
                if (constrainExist != null &&
                    constrainExist.toString().contains("NOIDUNGGOPY_VANBAN_FK")) {
                    Utils.showPopup(popupValidate);
                }
                this.unZipFile = false;
                hasFile = false;
                currentFiles.clear();
                vo.setAccessMode(RowSet.RANGE_PAGING);
                //Utils.iteratorRefresh("DtvbNoidungvanbanView1Iterator");
                Utils.iteratorRefresh("DtvbNoidungvanbanCheckExistView1Iterator");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void openPopupDelete(ActionEvent actionEvent) {
        if (Utils.getIteratorBinding("DtvbNoidungvanbanView1Iterator").getCurrentRow() ==
            null) {
            String messageText = "Không có văn bản nào.";
            Utils.showMessage(Utils.MSG_INFO, messageText);
        } else {
            Utils.showPopup(popupDeleteVb);
        }
    }

    public void DonviValueChanged(ValueChangeEvent valueChangeEvent) {
        Utils.setElExpression("#{bindings.DtvbDonviView1.inputValue}",
                              valueChangeEvent.getNewValue());
    }

    public void setPopupThemMoi(RichPopup popupThemMoi) {
        this.popupThemMoi = popupThemMoi;
    }

    public RichPopup getPopupThemMoi() {
        return popupThemMoi;
    }

    public void setFile(UploadedFile _file) {
        this._file = _file;
    }

    public UploadedFile getFile() {
        return _file;
    }

    public void setInputtextOperator(RichOutputText inputtextOperator) {
        this.inputtextOperator = inputtextOperator;
    }

    public RichOutputText getInputtextOperator() {
        return inputtextOperator;
    }

    public void setSohieuvb(String sohieuvb) {
        this.sohieuvb = sohieuvb;
    }

    public String getSohieuvb() {
        return sohieuvb;
    }

    public void setTrichyeu(String trichyeu) {
        this.trichyeu = trichyeu;
    }

    public String getTrichyeu() {
        return trichyeu;
    }

    public void setButtonDownload(RichCommandButton buttonDownload) {
        this.buttonDownload = buttonDownload;
    }

    public RichCommandButton getButtonDownload() {
        return buttonDownload;
    }

    public void setNgaybanhanhbd(Date ngaybanhanhbd) {
        this.ngaybanhanhbd = ngaybanhanhbd;
    }

    public Date getNgaybanhanhbd() {
        return ngaybanhanhbd;
    }

    public void setNgaybanhanhkt(Date ngaybanhanhkt) {
        this.ngaybanhanhkt = ngaybanhanhkt;
    }

    public Date getNgaybanhanhkt() {
        return ngaybanhanhkt;
    }

    public void setSelectdonvi(RichSelectOneChoice selectdonvi) {
        this.selectdonvi = selectdonvi;
    }

    public RichSelectOneChoice getSelectdonvi() {
        return selectdonvi;
    }

    public void setUnZipFile(boolean unZipFile) {
        this.unZipFile = unZipFile;
    }

    public boolean isUnZipFile() {
        return unZipFile;
    }

    public void setFileDeleted(String fileDeleted) {
        this.fileDeleted = fileDeleted;
    }

    public String getFileDeleted() {
        return fileDeleted;
    }

    public void setIter(UIComponent iter) {
        this.iter = iter;
    }

    public UIComponent getIter() {
        return iter;
    }

    public static void setHasFile(boolean hasFile) {
        noidungvbBean.hasFile = hasFile;
    }

    public static boolean isHasFile() {
        return hasFile;
    }

    public void setFileExisted(boolean fileExisted) {
        this.fileExisted = fileExisted;
    }

    public boolean isFileExisted() {
        int operator = inputtextOperator.getValue().equals("1") ? 1 : 0;
        if (operator == 1 && hasFile == true) {
            fileExisted = true;
        }
        return fileExisted;
    }

    public void setPopupXemVb(RichPopup popupXemVb) {
        this.popupXemVb = popupXemVb;
    }

    public RichPopup getPopupXemVb() {
        return popupXemVb;
    }

    public void setTitle(String Title) {
        this.Title = Title;
    }

    public String getTitle() {
        Title = "Thêm mới văn bản";
        int operator = inputtextOperator.getValue().equals("1") ? 1 : 0;
        if (operator == 0) {
            Title = "Thêm mới văn bản";
        } else {
            Title = "Sửa văn bản";
        }
        return Title;
    }

    public void setTxtSohieuvb(RichInputText txtSohieuvb) {
        this.txtSohieuvb = txtSohieuvb;
    }

    public RichInputText getTxtSohieuvb() {
        return txtSohieuvb;
    }

    public void rowSelected(SelectionEvent se) {
        Utils.rowSelected(se, "DtvbNoidungvanbanView1");
    }

    public String goToPage() {
        Utils.goToPage(RANGE_SIZE, pageNumber, "DtvbNoidungvanbanView1");
        return null;
    }

    public String goLastPage() {
        Utils.goLastPage(RANGE_SIZE, "DtvbNoidungvanbanView1");
        return null;
    }

    public boolean isLastEnabled() {
        return Utils.isLastEnabled(RANGE_SIZE, "DtvbNoidungvanbanView1");
    }

    public boolean isFirstEnabled() {
        return Utils.isFirstEnabled(RANGE_SIZE, "DtvbNoidungvanbanView1");
    }
    
    private JUCtrlRangeBinding getVanBanView() {
        return Utils.getViewObject("DtvbNoidungvanbanView1");
    }

    public void setPageNumber(int pageNumber) {
        JUCtrlRangeBinding VanBanView = this.getVanBanView();
        if (pageNumber >
            Math.max(1, (VanBanView.getIteratorBinding().getEstimatedRowCount() -
                         1) / RANGE_SIZE + 1)) {
            pageNumber =
                    (int)Math.max(1, (VanBanView.getIteratorBinding().getEstimatedRowCount() -
                                      1) / RANGE_SIZE + 1);
        }
        this.pageNumber = pageNumber;
    }

    public int getPageNumber() {
        return pageNumber =
                getVanBanView().getIteratorBinding().getNavigatableRowIterator().getRangeStart() /
                RANGE_SIZE + 1;
    }

    public static int getRANGE_SIZE() {
        return RANGE_SIZE;
    }

    public void setPopupDeleteVb(RichPopup popupDeleteVb) {
        this.popupDeleteVb = popupDeleteVb;
    }

    public RichPopup getPopupDeleteVb() {
        return popupDeleteVb;
    }

    public void setPopupValidate(RichPopup popupValidate) {
        this.popupValidate = popupValidate;
    }

    public RichPopup getPopupValidate() {
        return popupValidate;
    }

    public void setTbl(UIXTable tbl) {
        this.tbl = tbl;
    }

    public UIXTable getTbl() {
        return tbl;
    }
}
